<?php

namespace app\site\controller\index;

use app\admin\service\site\SiteUserRuleService;
use app\common\controller\CommonController;
use app\common\exception\MiException;
use app\common\service\CommonAreaService;
use app\model\site\SiteUser;
use app\model\site\SiteUserRule;
use app\model\sys\Area;
use app\site\middleware\CheckLogin;
use app\site\service\auth\SiteUserService;
use app\site\service\delivery\DeliveryExpressService;
use app\site\service\RequestService;
use app\site\service\site\AreaService;
use app\site\service\site\ArticleCateService;
use app\site\service\SiteReqService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Request;
use think\response\Json;
use function jsonSuccess;

class SiteController extends CommonController
{

    /**
     * 只需要验证登录，不需要验证权限的请求
     */
    protected array $middleware = [CheckLogin::class];


    /**
     * 站点管理员权限选择器
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function siteUserRules(): Json
    {
        $data = SiteUserRule::field('id,pid,name,title,sort')
            ->where('status', 1)
            ->where('rule_type', 'in', [1,2])
            ->order('sort desc')
            ->select();
        $options = (new SiteUserRuleService())->childrenTree($data);
        return jsonSuccess([
            'options' => $options
        ]);
    }

    /**
     * 文章分类选择器
     * @param ArticleCateService $service
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function articleCates(ArticleCateService $service): Json
    {
        $list = $service->setStatus(1)->select();
        $tree = $service->childrenTree($list);
        return jsonSuccess([
            'options' => $tree
        ]);
    }

    /**
     * 编辑个人信息
     * @return Json|void
     * @throws MiException
     */
    public function ownInfoEdit(){
        $post = $this->post();
        $ser = SiteUserService::instance();
        return SiteReqService::instance()->setService($ser)->edit($post)->getResponse();
    }

    /**  地区级联选项
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws DataNotFoundException
     */
    public function areaOptions(Request $request): \think\response\Json
    {
        $pid = $request->param('pid');
        $options  = Area::field('id,title,pid')->where('pid', $pid)
            ->select()
            ->toArray();
        // 转为索引数组
        $options = array_values($options);
        return jsonSuccess([
            'options' => $options
        ]);
    }

    /**
     * 所有地区的多维树形数组
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function areaAllChildrenTree(): Json
    {

        $tree = CommonAreaService::instance()->getAllChildrenTree();
        return jsonSuccess([
            'tree' => $tree
        ]);
    }

    /**
     * 快递公司选择器
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function expressOptions(): Json
    {
        $list = DeliveryExpressService::instance()->select();
        return jsonSuccess([
            'options' => $list,
        ]);
    }
}
